package com.example.zk1.presenter;

import android.util.Log;

import com.example.mvp.presenter.BasePresenter;
import com.example.mvp.view.IView;
import com.example.net.BaseObserable;
import com.example.net.BaseObserver;
import com.example.zk1.bean.AppBean;
import com.example.zk1.bean.LogBean;
import com.example.zk1.bean.RegBean;
import com.example.zk1.contract.LoginContract;

import java.util.HashMap;

public class LoginPresenter extends BasePresenter<LoginContract.LoginModel, IView> implements LoginContract.LoginPresenter {
    public LoginContract.AppView appView;
    public LoginContract.RegisterView registerView;
    public LoginContract.LoginView loginView;

    public LoginPresenter(LoginContract.LoginModel mmodel,LoginContract.AppView appView) {
        super(mmodel, appView);
        this.appView = appView;
    }

    public LoginPresenter(LoginContract.LoginModel mmodel, LoginContract.RegisterView registerView) {
        super(mmodel, registerView);
        this.registerView = registerView;
    }

    public LoginPresenter(LoginContract.LoginModel mmodel,LoginContract.LoginView loginView) {
        super(mmodel, loginView);
        this.loginView = loginView;
    }

    @Override
    public void requestApp() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name",appView.getUsername());
        map.put("passwd",appView.getpwd());
        map.put("email",appView.getEmail());
        new BaseObserable().doIt(mmodel.appMed(map),new BaseObserver<AppBean>(){
            @Override
            public void onNext(AppBean appBean) {
                super.onNext(appBean);
                if(appBean.getCode()==200){
                    Log.e("xxr", "onNext: 获取apikey"+appBean);
                    Log.e("xxr", "onNext: 获取apikey:::"+appBean.getResult().getApikey());
                    appView.goMain();
                }else{
                    Log.e("xxr", "onNext:未获取到apikey " );
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("xxr", "onError: 失败"+e.getMessage() );
            }
        });

    }

    @Override
    public void requestRegister() {
        HashMap<String, String> map = new HashMap<>();
        map.put("apikey",registerView.getApikey());
        map.put("name",registerView.getUsername());
        map.put("passwd",registerView.getpwd());

        new BaseObserable().doIt(mmodel.regMed(map),new BaseObserver<RegBean>(){
            @Override
            public void onNext(RegBean regBean) {
                super.onNext(regBean);
                if(regBean.getCode()==200){
                    Log.e("xxr", "onNext:注册成功 "+regBean);
                    registerView.goMain();
                }else{
                    Log.e("xxr", "onNext:没有注册成功 ");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("xxr", "onNext:注册失败 ");
            }
        });

    }

    @Override
    public void requestLogin() {
        HashMap<String, String> map = new HashMap<>();
        map.put("apikey",loginView.getApikey());
        map.put("name",loginView.getUsername());
        map.put("passwd",loginView.getpwd());

        new BaseObserable().doIt(mmodel.logMed(map),new BaseObserver<LogBean>(){
            @Override
            public void onNext(LogBean logBean) {
                super.onNext(logBean);
                if(logBean.getCode()==200){
                    Log.e("xxr", "onNext:登录成功 "+logBean);
                    loginView.goMain();
                }else{
                    Log.e("xxr", "onNext:没有登录成功 ");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("xxr", "onNext:登录失败 ");
            }
        });

    }
}
